
	
*** DUOPOLY/TRIOPOLY MARKETS 


use "$P_Data_Processed/daily_station_data_e5_7_21.dta", clear

drop if Post==0
	
gegen daily_market_price = mean(AvgP), by(Post date)
gegen daily_market_wh_margin = mean(margin_wholesale), by(Post date)
	
gegen market_mean_wh_margin = mean(daily_market_wh_margin), by(Post year_month)

gegen market_mean_price = mean(daily_market_price), by(Post year_month)
				
by StID year_month, sort: keep if _n==1 
	
by year_month Post, sort: gener postal_n_stations = _N
gener postal_n_others = postal_n_stations-1
drop if Post==0

capture drop treat
gener treat = month_treat_period*treat_group

drop if mean_price<0
drop if market_mean_wh_margin<0 
drop if market_mean_price<1.2
drop if market_mean_price>1.6

sort StID year_month
by StID: gener first_treat_month = year_month if treat==1 & treat[_n-1]==0
sort StID first_treat_month
by StID: replace first_treat_month = first_treat_month[1]
replace first_treat_month = 1000 if missing(first_treat_month)


** generating simple IV - share of adopters in your brand
by Brand year_month, sort: gener n_brand = _N
by Brand year_month: egen n_treat = total(treat)

gener share_others_treated = (n_treat)/(n_brand-1) if treat==0
replace share_others_treated = (n_treat-1)/(n_brand-1) if treat==1

* keeping only 2/3 station mkts 
keep if postal_n_stations==2 | postal_n_stations==3

* drops mkts that used to have 1 station 
by Post year_month, sort: gener n_obs = _N
by Post, sort: egen min_n_obs = min(n_obs)
drop if min_n_obs==1
drop min_n_obs
drop n_obs
	
	
sort Post year_month StID
by Post year_month : gen id=_n
	
capture drop T1
capture drop T2
capture drop IV1
capture drop IV2
	
* calculating market level treatment effects / instruments
by Post year_month: gen T1 = treat[1]*(1-treat[2])+treat[2]*(1-treat[1]) if postal_n_stations==2
by Post year_month: replace T1 = treat[1]*treat[2]*(1-treat[3]) + treat[1]*treat[3]*(1-treat[2]) + treat[2]*treat[3]*(1-treat[1]) + treat[1]*(1-treat[2])*(1-treat[3])+treat[2]*(1-treat[1])*(1-treat[3]) +treat[3]*(1-treat[1])*(1-treat[2]) if postal_n_stations==3
								
by Post year_month: gen T2 = treat[1]*treat[2] if postal_n_stations==2
by Post year_month: replace T2 = treat[1]*treat[2]*treat[3] if postal_n_stations==3
	
		
label variable T1 "Not all Stations Adopted"
label variable T2 "All Stations Adopted"
label variable market_mean_wh_margin "Mean Market Wholesale Margin"
label variable market_mean_price "Mean Market Price"

by Post year_month: gen IV1 = share_others_treated[1]*(1-share_others_treated[2])+share_others_treated[2]*(1-share_others_treated[1]) if postal_n_stations==2
by Post year_month: replace IV1 = share_others_treated[1]*(1-share_others_treated[2])*(1-share_others_treated[3])+share_others_treated[2]*(1-share_others_treated[1])*(1-share_others_treated[3]) + share_others_treated[3]*(1-share_others_treated[1])*(1-share_others_treated[2])  ///
									+  share_others_treated[1]*share_others_treated[2]*(1-share_others_treated[3]) +  share_others_treated[1]*share_others_treated[3]*(1-share_others_treated[2]) +  share_others_treated[2]*share_others_treated[3]*(1-share_others_treated[1]) ///
									 if postal_n_stations==3
	
by Post year_month: gen IV2 = share_others_treated[1]*share_others_treated[2] if postal_n_stations==2
by Post year_month: replace IV2 = share_others_treated[1]*share_others_treated[2]*share_others_treated[3]  if postal_n_stations==3
		
by Post year_month: gen n_brand1 = n_brand[1]
by Post year_month: gen n_brand2 = n_brand[2]
by Post year_month: gen n_brand3 = n_brand[3]
replace n_brand3 = 0 if postal_n_stations==2
	
by Post year_month: gener postal_n_others1 = postal_n_others[1]
by Post year_month: gener postal_n_others2 = postal_n_others[2]
 by Post year_month: gener postal_n_others3 = postal_n_others[3]
replace postal_n_others3 = 0 if postal_n_stations==2

by Post year_month: gener use_mkt= 1 if _n==1

ivreghdfe market_mean_wh_margin l_gdp pop_density med_age employed_share l_pop  n_brand1 n_brand2 mean_temp sd_temp mean_precip sd_precip (T1 T2 = IV1 IV2) if  use_mkt==1 & n_brand1>5, absorb(Post year_month)  cluster(Post) 
 outreg2 using "$P_Tables/Table_G3",  tex(frag) dec(3) label nor2 replace  keep(T1 T2)  

ivreghdfe market_mean_price l_gdp pop_density med_age employed_share l_pop  n_brand1 n_brand2 mean_temp sd_temp mean_precip sd_precip (T1 T2 = IV1 IV2) if  use_mkt==1& n_brand1>5, absorb(Post year_month)  cluster(Post) 
 outreg2 using "$P_Tables/Table_G3",  tex(frag) dec(3) label nor2 append  keep(T1 T2)  
